package com.todo.strategy.pipeline.application.convert;

import com.todo.common.utils.TodoCommonUtils;
import com.todo.strategy.pipeline.application.entity.bo.TransferOriginDataBO;
import com.todo.strategy.pipeline.application.entity.context.StrategyActionContext;
import com.todo.strategy.pipeline.common.enums.ExecuteStatusEnum;
import com.todo.strategy.pipeline.infrastructure.entity.po.PipelineRecordPO;

import java.util.Date;

/**
 * @author azhebuxing
 * @date 2025/3/1 05:58
 * @description
 */
public class PipelineRecordConvert {
    public static PipelineRecordPO buildIndertPipelineRecordPO(TransferOriginDataBO transferOriginDataBO) {
        if (transferOriginDataBO == null) {
            return null;
        }
        PipelineRecordPO pipelineRecordPO = new PipelineRecordPO();
        pipelineRecordPO.setId(TodoCommonUtils.buildLongGuid());
        pipelineRecordPO.setUniKey(transferOriginDataBO.getUniKey());
        pipelineRecordPO.setActionType(transferOriginDataBO.getStrategyActionEnum().getType());
        pipelineRecordPO.setOperateType(transferOriginDataBO.getOperateActionEnum().getType());
        pipelineRecordPO.setBusinessId(transferOriginDataBO.getBusinessId());
        pipelineRecordPO.setOriginData(transferOriginDataBO.getOriginData());
        pipelineRecordPO.setOperateVersion(transferOriginDataBO.getCurrentVersion());
        pipelineRecordPO.setRecordStatus(ExecuteStatusEnum.POST.getType());
        pipelineRecordPO.setIsDelete(0);
        pipelineRecordPO.setCreateTime(new Date());
        pipelineRecordPO.setUpdateTime(new Date());
        return pipelineRecordPO;
    }

    public static StrategyActionContext buildStrategyActionContext(TransferOriginDataBO transferOriginDataBO) {
        if (transferOriginDataBO == null) {
            return null;
        }
        StrategyActionContext strategyActionContext = new StrategyActionContext();
        strategyActionContext.setUniKey(transferOriginDataBO.getUniKey());
        strategyActionContext.setStrategyActionEnum(transferOriginDataBO.getStrategyActionEnum());
        strategyActionContext.setOperateActionEnum(transferOriginDataBO.getOperateActionEnum());
        strategyActionContext.setBusinessId(transferOriginDataBO.getBusinessId());
        strategyActionContext.setOriginData(transferOriginDataBO.getOriginData());
        strategyActionContext.setEnableVersion(transferOriginDataBO.getEnableVersion());
        strategyActionContext.setCurrentVersion(transferOriginDataBO.getCurrentVersion());
        return strategyActionContext;
    }

    public static PipelineRecordPO buildSuccessPipelineRecordPO(PipelineRecordPO originalPipelineRecordPO) {
        if (originalPipelineRecordPO == null) {
            return null;
        }
        PipelineRecordPO pipelineRecordPO = new PipelineRecordPO();
        pipelineRecordPO.setId(originalPipelineRecordPO.getId());
        pipelineRecordPO.setRecordStatus(ExecuteStatusEnum.SUCCESS.getType());
        pipelineRecordPO.setSuccessTime(new Date());
        pipelineRecordPO.setUpdateTime(new Date());
        return pipelineRecordPO;
    }

    public static PipelineRecordPO buildFailedPipelineRecordPO(PipelineRecordPO originalPipelineRecordPO, String errorMsg) {
        if (originalPipelineRecordPO == null) {
            return null;
        }
        PipelineRecordPO pipelineRecordPO = new PipelineRecordPO();
        pipelineRecordPO.setId(originalPipelineRecordPO.getId());
        pipelineRecordPO.setRecordStatus(ExecuteStatusEnum.FAILED.getType());
        pipelineRecordPO.setErrorMsg(errorMsg);
        pipelineRecordPO.setUpdateTime(new Date());
        return pipelineRecordPO;
    }
}
